package com.example.androidcoursedesign.main;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.androidcoursedesign.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity  {
    private BottomNavigationView mBottomNav;
    private List<Fragment> fragmentList;
    // 当前显示的fragment
    private int mPreFragmentFlag = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化view
        initView();
        //初始化Fragment相关配置
        initFragment();
        selectFragment();

    }



    private void initView() {
        //隐藏ActionBar
       getSupportActionBar().hide();
        mBottomNav= findViewById(R.id.mBottomNav);
    }


    private void initFragment() {
        fragmentList=   new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        homeFragment.setContext(this);
        DiscoveryFragment discoveryFragment = new DiscoveryFragment();
        MyFragment myFragment = new MyFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(discoveryFragment);
        fragmentList.add(myFragment);
        initLoadFragment(R.id.container_id,0,fragmentList);
    }

    private void initLoadFragment(int container_id, int showFragment, List<Fragment> fragmentList) {
        //获取到FragmentManager实例的同时去开启事物
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            //首先将Fragment添加到事务中
            fragmentTransaction.add(container_id,fragmentList.get(i),fragmentList.get(i).getClass().getName());
            //默认展示 fragmentList.get(showFragment)
            //这里做首次Fragment的展示，如果不是指定的Fragment就先隐藏，需要的时候再显示出来
            if (i!=showFragment){
                fragmentTransaction.hide(fragmentList.get(i));
            }
        }
        //提交事务
        fragmentTransaction.commit();
    }
    private void selectFragment() {
        //设置icon的颜色
        mBottomNav.setItemIconTintList(null);
        //注册监听事件
        mBottomNav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();
                switch (itemId){
                    case R.id.home:
                        showAndHideFragment(fragmentList.get(0),fragmentList.get(mPreFragmentFlag));
                        mPreFragmentFlag=0;
                        break;
                    case R.id.discovery:
                        showAndHideFragment(fragmentList.get(1),fragmentList.get(mPreFragmentFlag));
                        mPreFragmentFlag=1;
                        break;
                    case R.id.my:
                        showAndHideFragment(fragmentList.get(2),fragmentList.get(mPreFragmentFlag));
                        mPreFragmentFlag=2;
                    break;
                }
                return true;
            }
        });


    }
    //加载不同的Fragment
    private void showAndHideFragment(Fragment show, Fragment hide) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (show != hide){
            //显示选择的fragment
             transaction.show(show);
             //隐藏当前显示fragment
             transaction.hide(hide);

                     //.commitAllowingStateLoss();
            transaction.commit();
        }


    }


}